package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;

import snaq.db.ConnectionPool;


import com.mysql.jdbc.Driver;

public class DbConnection {
	private static ConnectionPool pool;

	public static Connection getConnect() {
		if (pool != null) {
			try {
				return pool.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
				synchronized (DbConnection.class) {
					if (pool == null) {
						createPool();
					}
				}
			return DbConnection.getConnect();
		}
		return null;
	}

	public synchronized static void createPool() {
			String url = "jdbc:mysql://localhost/webgame";
		String user = "root";
		String pass = "853076";
		Connection connection = null;
		try {
			Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver")
					.newInstance();
			DriverManager.registerDriver(driver);
			// Note, timeout is specified in milliseconds.
			pool = new ConnectionPool("local", 5, 10, 30, 180000, url, user,
					pass);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
